package com.fos.BluetoothStory.adapter;

import com.fos.BluetoothStory.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/**
 * 菜单adapter
 * @author fanxing 创建于 Oct 23, 2015
 */
public class MenuAdapter extends BaseAdapter{
	private final static int COUNT = 5;
	
	private LayoutInflater inflater;
	private Resources resources;
	private int lastSelected = 4;
	private String pkn;
	
	public MenuAdapter(Context ctx) {
		inflater = LayoutInflater.from(ctx);
		resources = ctx.getResources();
		pkn = ctx.getPackageName();
	}

	@Override
	public int getCount() {
		return COUNT;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * 数据少，不做重用
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(position == 3){
			return new View(parent.getContext());
		}
		convertView = inflater.inflate(R.layout.adapter_item_menu, parent, false);
		ViewHolder holder = new ViewHolder();
		holder.iconIv = (ImageView) convertView.findViewById(R.id.iv);
		holder.tagView = convertView.findViewById(R.id.view);
		
		holder.iconIv.setImageResource(resources.getIdentifier("menu_"+position, "drawable", pkn));
		
		if(lastSelected == position){
			holder.tagView.setBackgroundResource(resources.getIdentifier("triangle_"+position, "drawable", pkn));
		}else{
			holder.tagView.setBackgroundColor(Color.TRANSPARENT);
		}
		
		convertView.setTag(holder);
		
		return convertView;
	}

	private class ViewHolder{
		public ImageView iconIv;
		public View tagView;
	}

	public void changeSelect(int position) {
		lastSelected = position;
		notifyDataSetChanged();
	}
}
